﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace Web.Admin.Admins
{
    public partial class Pages_Admin_Admins_UserRolesControl : System.Web.UI.UserControl
    {
        public string UserName
        {
            get
            {
                return ((string)(ViewState["UserName"]));
            }
            set
            {
                ViewState["UserName"] = value;
                PopulateRoles();
                foreach (string role in Roles.GetRolesForUser(value))
                {
                    ListItem item = CheckBoxList1.Items.FindByValue(role);
                    item.Selected = true;
                }
            }
        }

        public bool ReadOnly
        {
            get
            {
                return !CheckBoxList1.Enabled;
            }
            set
            {
                CheckBoxList1.Enabled = !value;
            }
        }

        public ListItemCollection AllRoles
        {
            get
            {
                return CheckBoxList1.Items;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateRoles(false);
        }

        public void PopulateRoles()
        {
            PopulateRoles(true);
        }

        protected void PopulateRoles(bool force)
        {
            if (force)
                CheckBoxList1.Items.Clear();
            if (CheckBoxList1.Items.Count == 0)
            {
                foreach (string role in Roles.GetAllRoles())
                {
                    CheckBoxList1.Items.Add(role);
                }
            }
        }
    }
}
